package com.lxs.legou.item.controller;

import com.lxs.legou.core.controller.BaseController;
import com.lxs.legou.core.po.BaseTreeEntity;
import com.lxs.legou.item.po.Brand;
import com.lxs.legou.item.service.IBrandService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/item/brand")
@CrossOrigin
public class BrandController extends BaseController<IBrandService, Brand> {

    @Override
    public void afterEdit(Brand entity) {
        List<BaseTreeEntity> list =  service.selectTag(entity.getId());
        Long[] categoryIds = new Long[list.size()];
        for (int i = 0; i < list.size(); i++) {
            categoryIds[i]=list.get(i).getId();
        }
        entity.setCategoryIds(categoryIds);
    }

    @Override
    @Transactional
    public void beforeSave(Brand entity) throws Exception {
        List<BaseTreeEntity> list =  service.selectTag(entity.getId());
        service.delTree(entity.getId());
        Long[] categoryIds = entity.getCategoryIds();
        for (int i = 0; i < categoryIds.length; i++) service.insertTree(entity.getId(),categoryIds[i]);
    }
}
